package com.shell.major_course;

import android.content.DialogInterface;
import android.content.SearchRecentSuggestionsProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText major,course;
private Button buttonExit,buttonMajor,buttonLogin,buttonCourse;
String []majors=new String[]{"计算机科学与技术","软件工程","数据科学与大数据技术","物联网工程"};
String []classes=new String[]{"编译原理","操作系统","Android应用开发","Java","C/C++"};
boolean [] itemSelected=new boolean[]{false,false,false,false,false}; //单选框状态
int majorID;//用于保存majors数组被选中的元素的下标
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
        major=findViewById(R.id.major);
        course=findViewById(R.id.course);
        buttonMajor=findViewById(R.id.choseMajor);
        buttonMajor.setEnabled(false);//选专业按钮登陆之前不允许点击
        buttonMajor.setOnClickListener(this);
        buttonCourse=findViewById(R.id.choseClass);
        buttonCourse.setEnabled(false);//选课按钮登陆之前不允许点击
        buttonCourse.setOnClickListener(this);
        buttonLogin=findViewById(R.id.buttonLogin);
        buttonLogin.setOnClickListener(this);
        buttonExit=findViewById(R.id.buttonExit);
        buttonExit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonExit:showNormalDialog();break;
            case R.id.choseMajor:showSingleDialog();break;
            case R.id.choseClass:showSelectClass();break;
            case R.id.buttonLogin:login();break;
        }
    }
    public void showNormalDialog(){//点击“退出”
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);//设置图标
        builder.setTitle("普通对话框");//设置标题
        builder.setMessage("确定退出？");//设置提示信息
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭当前对话框
            }
        });//取消
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭对话框
                MainActivity.this.finish();//退出程序
            }
        });//确定
        builder.show(); //显示对话框
    }
    public void showSingleDialog(){//点击“选择专业”
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);//设置图标
        builder.setTitle("请选择专业");//设置标题
        //参数：数组，默认选中项下标，监听器
        builder.setSingleChoiceItems(majors, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) { //第二个参数保存的是选中的项
                majorID=which;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭当前对话框
            }
        });//取消
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                major.setText(majors[majorID]);//将选择的信息显示在文本框中
                dialog.dismiss();//关闭当前对话框
            }
        });//确定
        builder.show();
    }
    public void showSelectClass(){//点击“选择课程”
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);//设置图标
        builder.setTitle("选择课程");//设置标题
        //参数：数组，布尔型数组，监听器。数组记录复选框内容，布尔型数组保存状态，初始化全false，以true标注为选中状态
        builder.setMultiChoiceItems(classes, itemSelected, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                itemSelected[which]=isChecked;//利用布尔型数组，保存每个复选框的状态
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭当前对话框
            }
        });//取消
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuilder stringBuilder=new StringBuilder();//可变字符串
                for (int i=0;i<itemSelected.length;i++){
                    if (itemSelected[i]){ //itemSelected数组默认全是false，在102行左右改变状态为true后表示选中
                        stringBuilder.append(classes[i]+" ");
                    }
                }
                course.setText(stringBuilder);//显示信息
                dialog.dismiss();//关闭对话框
            }
        });//确定
        builder.show();
    }
    public void login(){//点击“登录”
        AlertDialog.Builder builder=new AlertDialog.Builder(this); //新建对话框
        builder.setIcon(R.mipmap.ic_launcher);//设置图标
        builder.setTitle("登陆");
        View view= LayoutInflater.from(this).inflate(R.layout.layout_login,null);//从布局填充器填充
        builder.setView(view); //放置视图
        final EditText name=view.findViewById(R.id.username);//获取layout_login.xml中的username
        final EditText pwd=view.findViewById(R.id.password);//获取layout_login.xml中的password

        builder.setPositiveButton("登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String username=name.getText().toString();//获取输入的用户名
                String password=pwd.getText().toString();//获取输入的密码
                if ("admin".equals(username)&&"123456".equals(password)){
                    buttonMajor.setEnabled(true);//选专业按钮可以点击
                    buttonCourse.setEnabled(true);//选课按钮可以点击
                    Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
//                    dialog.dismiss();//隐藏对话框
                }else {
                    Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                    buttonMajor.setEnabled(false);//重新登陆时，一旦登陆失败就不允许点击
                    buttonCourse.setEnabled(false);
                }
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//隐藏对话框
            }
        });
        builder.show();//显示当前自定义对话框

    }
}
